package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author Xiao
 * @version 1.0
 * @description 小视频Controller
 * @date 2021/11/2
 */
@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {
    @Autowired
    private SmallVideosService smallVideosService;

    /**
     * 小视频-视频上传
     * @param videoThumbnail 视频封面文件
     * @param videoFile 视频文件
     * @return
     */
    @PostMapping
    public ResponseEntity uploadVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        smallVideosService.uploadVideo(videoThumbnail, videoFile);
        return ResponseEntity.ok("发布成功！");
    }

    /**
     * 小视频-列表查询
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity getVideos(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = smallVideosService.getVideos(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 小视频-用户关注/取消关注
     * @param followUserId  要(取消)关注的用户id
     * @param focusParam    userFocus(关注)/userUnFocus(取消关注)
     * @return
     */
    @PostMapping("/{uid}/{focusParam}")
    public ResponseEntity focus(@PathVariable("uid") Long followUserId,
                                @PathVariable("focusParam") String focusParam) {
        Boolean choose = "userFocus".equals(focusParam);
        smallVideosService.focus(followUserId, choose);
        if (choose) {
            return ResponseEntity.ok("已关注！");
        }
        return ResponseEntity.ok("已取消关注！");
    }

    /**
     * 小视频-点赞
     * @param id 要点赞的视频id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable String id) {
        smallVideosService.like(id, true);
        return ResponseEntity.ok("已点赞！");
    }

    /**
     * 小视频-取消点赞
     * @param id 要取消点赞的视频id
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable String id) {
        smallVideosService.like(id, false);
        return ResponseEntity.ok("已取消点赞！");
    }

    /**
     * 小视频-评论列表
     * @param id    视频id
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity getComments(@PathVariable String id,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = smallVideosService.getComments(id, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 小视频-发布评论
     * @param id    视频id
     * @param map   [comment,"评论正文"]
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity publishComment(@PathVariable String id,
                                         @RequestBody Map map) {
        String content = (String) map.get("comment");
        smallVideosService.publishComment(id, content);
        return ResponseEntity.ok("评论成功！");
    }

    /**
     * 小视频-评论点赞/取消点赞
     * @param id    评论id
     * @param likeParam like(点赞)/dislike(取消点赞)
     * @return
     */
    @PostMapping("/comments/{id}/{likeParam}")
    public ResponseEntity commentsLike(@PathVariable String id,
                                       @PathVariable String likeParam) {
        Boolean choose = "like".equals(likeParam);
        smallVideosService.commentsLike(id, choose);
        if (choose) {
            return ResponseEntity.ok("已点赞！");
        }
        return ResponseEntity.ok("已取消点赞");
    }
}
